package com.fulihui.wgj.admin.api.subscribereply;

import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.api.base.BaseController;
import com.fulihui.wgj.admin.api.subscribereply.request.AdvertIdRequest;
import com.fulihui.wgj.admin.api.subscribereply.request.SubscribeAdvertEditRequest;
import com.fulihui.wgj.admin.api.subscribereply.request.SubscribeAdvertPageRequest;
import com.fulihui.wgj.admin.api.subscribereply.vo.SubscribeReplyAdvertVO;
import com.fulihui.wgj.admin.integration.SubscribeAdverseServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: Levon
 * @date: 2018-07-26 13:59
 */
@RestController
@RequestMapping("/advert")
@Api(tags = "广告库相关接口")
public class AdvertController extends BaseController {

    @Autowired
    private SubscribeAdverseServiceClient subscribeAdverseServiceClient;


    @ApiOperation(value = "保存广告库")
    @PostMapping("/save")
    public Result save(@RequestBody @Valid SubscribeAdvertEditRequest request, BindingResult validResult,
                       HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return subscribeAdverseServiceClient.edit(request);
    }

    @ApiOperation(value = "修改广告库")
    @PostMapping("/update")
    public Result edit(@RequestBody @Valid SubscribeAdvertEditRequest request, BindingResult validResult,
                       HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return subscribeAdverseServiceClient.edit(request);
    }

    @ApiOperation(value = "删除广告库")
    @PostMapping("/delete")
    public Result delete(@RequestBody @Valid AdvertIdRequest request, BindingResult validResult) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        return subscribeAdverseServiceClient.del(request.getId());
    }


    @ApiOperation(value = "分页查询广告库列表")
    @PostMapping("/page")
    public Result<PageView<SubscribeReplyAdvertVO>> page(@RequestBody @Valid SubscribeAdvertPageRequest request, BindingResult validResult,
                                                         HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        PageView<SubscribeReplyAdvertVO> pageView = subscribeAdverseServiceClient.queryPage(request);
        return JsonResultBuilder.succ(pageView);
    }

    @ApiOperation(value = "根据主键id查询广告库")
    @PostMapping("/detail")
    public Result<SubscribeReplyAdvertVO> queryDetail(@RequestBody @Valid AdvertIdRequest request, BindingResult validResult) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        return subscribeAdverseServiceClient.queryById(request.getId());
    }
}
